package com.freesid.ask.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * 
 * @author jkyang
 * 
 */
public class Ask implements EntryPoint {

	public void onModuleLoad() {
		
		if(true) {
			
			TableGrid grid = new TableGrid("test");
			
			grid.load();
			
			RootPanel.get().add(grid);
			
			
			return ; 
		}

//		if (true) {

			Command menuCommand = new Command() {
				private int curPhrase = 0;
				private final String[] phrases = new String[] { "a", "b" };

				public void execute() {
					Window.alert(phrases[curPhrase]);
					curPhrase = (curPhrase + 1) % phrases.length;
				}
			};

			MenuBar menu = new MenuBar();
			menu.setAutoOpen(true);
			menu.setWidth("100%");
			menu.setAnimationEnabled(true);
			// Create the file menu
			MenuBar fileMenu = new MenuBar(true);
			fileMenu.setAnimationEnabled(true);
			menu.addItem(new MenuItem("file", fileMenu));
			String[] fileOptions = new String[] { "a", "b" };
			for (int i = 0; i < fileOptions.length; i++) {
				if (i == 3) {
					fileMenu.addSeparator();
					fileMenu.addItem(fileOptions[i], menuCommand);
					fileMenu.addSeparator();
				} else {
					fileMenu.addItem(fileOptions[i], menuCommand);
				}
			}
			RootPanel.get("floatMenu").add(menu);
			
//			for(int i=0;i<100;i++) {
//				RootPanel.get().add(new HTML(i+"<br>"));
//			}
			
			RootPanel.get("loading").setVisible(false);
//			return;
//		}

		final LoginPanel login = new LoginPanel();
		RootPanel.get().add(login);
		login.center();
		login.getSubmitButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				login.hide();
				GWT.runAsync(new RunAsyncCallback() {
					@Override
					public void onSuccess() {
						AbsolutePanel menu = new AbsolutePanel();
						ModifyPanel modify = new ModifyPanel("mainDiv");
						menu.add(modify);
						RootPanel rootPanel = RootPanel.get("menuDiv");
						rootPanel.add(menu);
					}

					@Override
					public void onFailure(Throwable reason) {
						Window.alert("Code download failed");
					}
				});
			}
		});
		RootPanel.get("loading").setVisible(false);
	}
}
